<?php
 /**
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version 2
   * of the License, or any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   *
   * ----------------------------------------------------------------------
   * NimWiki - designer centric wiki
   *
   * @author Nicolas Myers <myers.nicolas@gmail.com>
   * @copyright 2007 Nicolas Myers
   * @version 0.9.0
   * @link
   * ----------------------------------------------------------------------
   * WIKI_PLUGINS.PHP
   *
   * collection of functions/plugins used in the wiki syntax
   * ----------------------------------------------------------------------
   */
   
   /**
    * Returns found pages according to POST values
    * @return  string  xhtml list produced
    * @todo fix    
    */
    function plugin_search()
    {
        $CI=& get_instance();
        //show the search form, or the results?
        if (!$CI->input->post('search')) {
            //displays the form
            $output="";
            return $CI->load->view('/wiki/plugins/search.html.php',$output,true);
        } else {
            //displays the results
            $resultList=array();
            $search=explode(' ',' '.$CI->input->post('search'));
            $pages=$CI->wiki_model->getAllPages();
            foreach ($pages as $page) {
                $score=0;
                list($pagecontent,$pagemeta)=$CI->wiki_model->getRawAndMeta($page);
                foreach($search as $searchWord)
                    if($searchWord != "")
                        $score += substr_count($pagecontent, $searchWord);
                if ($score != 0)
                    $resultList[$page] = $score;
            }
            arsort($resultList);
            $output['search']=$CI->input->post('search');
            $output['resultList']=$resultList;
            return $CI->load->view('/wiki/plugins/search_results.html.php',$output,true);
        }
    }
    
   /**
    * Returns a list of pages recently changed in the wiki
    * ordered by date
    * @return  string  xhtml list produced
    */
    function plugin_recentchanges($nb)
    {        
        $CI=& get_instance();
        $pages=$CI->wiki_model->getAllPages();
        //gets all "last-modified" dates
        foreach ($pages as $page)
            $tmpmodified[$page]=filemtime($CI->wiki_model->getPageFilename($page));
        arsort($tmpmodified);
        //regroup by day
        $limit=empty($nb) ? 10: $nb;
        foreach ($tmpmodified as $page=>$date) {
            $modified[date("d/m/Y",$date)][$page]=$date;
            if ($limit--==0) break;
        }
        $output['pagesmodified']=$modified;
        return $CI->load->view('/wiki/plugins/recentchanges.html.php',$output,true);
    }

   /**
    * Returns an alphabeticaly ordered list of all the pages in the wiki
    * @return  string  xhtml list produced
    */
    function plugin_allpages()
    {
        $CI=& get_instance();
        $output['pages']=$CI->wiki_model->getAllPages();
        return $CI->load->view('/wiki/plugins/allpages.html.php',$output,true);
    }
    
    function plugin_contact($email)
    {
        //if there's no email it can't work!
        if (!isset($email)) return "<!-- no email-->";
        $CI=& get_instance();
        if (!$CI->input->post('sendmessage')) {
            //displays the form
            $output="";
            return $CI->load->view('/wiki/plugins/contact.html.php',$output,true);
        } else {
            $CI->load->library('validation');
            $rules['name']	  = "required";
    		$rules['email']   = "required|valid_email";
    		$rules['subject'] = "required";
    		$rules['message'] = "required";
		    $CI->validation->set_rules($rules);
		    $fields['name']    = 'Name';
            $fields['email']   = 'Email';
            $fields['subject'] = 'Subject';
            $fields['message'] = 'Message';
            $CI->validation->set_fields($fields);
		    if ($CI->validation->run() == false)
    		{
    			$output['message']=$CI->validation->error_string;
                return $CI->load->view('/wiki/plugins/contact.html.php',$output,true);
    		}
    		else
    		{
    		    $output['message']=$CI->validation->error_string;
                $CI->load->library('email');
                $CI->email->from($CI->input->post('email'), $CI->input->post('name'));
                $CI->email->to($email);
                $CI->email->subject($CI->input->post('subject'));
                $CI->email->message($CI->input->post('message'));
                $CI->email->send();
                $output['message']=$CI->email->print_debugger();
    			return $CI->load->view('/wiki/plugins/contact_success.html.php',$output,true);
    		}
        }
    }
    
    
   /**
    * Includes a page in another one (possible problem with loops!!)
    * @param  array   $pagename  pagename to include
    * @param  string  xhtml of the included page
    * @todo fix this!    
    */
    function plugin_include($pagename,$part="main")
    {
        $CI =& get_instance();
    	if (class_exists('wiki_model'))
    	return $CI->wiki_model->wiki2html($pagename,$part);
    }
    
    
?>
